<?php
namespace app\index\controller;

use app\common\model\AuthRuleSon as AuthRuleSonModel;
use app\common\model\NodeSon as NodeSon;
use app\common\model\ManagerDuty as ManagerDutyModel;
use app\common\model\Company as CompanyModel;
/**
 * 菜单控制器
 * Class Index
 * @package app\index\controller
 */
class AuthRuleSon extends Base
{
    /**
     * [index  获取菜单]
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $model = new AuthRuleSonModel;
        $param = input('param.');
        $user = $this->user;
        $node = new NodeSon();
        //获取现在的权限
        $nodeStr = $node->getMenu();
        writelog($user['mid'],$user['comid'],'获取菜单');

        return admin_json(1, 'success', $nodeStr);
    }
    /**
     * [postAdd  添加]
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new AuthRuleSonModel;
        try{
            $model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
    /**
     * [menu  获取自定义菜单]
     * @return json
     * @author [C。]
     */
    public function getMenu()
    {
        $user = $this->user;
        $model = new AuthRuleSonModel;
        $dmodel = new ManagerDutyModel;
        $cmodel = new CompanyModel;
        try{
            $top = $model->getAllData(['pid'=>0,'status'=>1],'sort','id,title,pid','asc');
            $role = $dmodel->findData($user['dutyid'],'id','rules');
            if(!empty($role)){
                $rule = explode(',', $role['rules']);
            }else{
                $rule = $role['rules'];
            }

            $company = $cmodel->findData($user['comid'],'id','modules');
            $company['modules'] = unserialize($company['modules']);
            $i=0;
            $data = [];
            if(!empty($company['modules'])){
                foreach($company['modules'] as $k=>$v){
                    if($v['is_show'] == 0 && !empty($v['id'])){
                        $data[$i] = $v['id'];
                        $i++;
                    }
                }
            }
            $menu = array();
            $i = 0;
            $j = 0;
            foreach($top as $key=>$vo) {
                $menu2 = [];
                if (!empty($rule) && in_array($vo['id'], $rule) && $user['dutyid'] != 2 && !in_array($vo['id'],$data)) {
                    $menu[$i] = $vo;
                    $result2 = $model->getAllData(['pid'=>$vo['id'],'status'=>1],'sort','id,title,pid','asc');
                    foreach ($result2 as $k1=>$vo1){
                        if(in_array($vo1['id'], $rule)){
                            $menu2[] = $vo1;
                        }
                    }
                    $menu[$i]['lv'] = $menu2;
                    $i++;
                }elseif($user['dutyid'] == 2){
                    if(!in_array($vo['id'],$data)){
                        $menu[$j] = $vo;
                        $result2 = $model->getAllData(['pid'=>$vo['id'],'status'=>1],'sort','id,title,pid','asc');
                        foreach($result2 as $k1=>$vo1){
                            if(!in_array($vo1['id'],$data)){
                                $menu2[] = $vo1;
                            }
                        }
                        $menu[$j]['lv'] = $menu2;
                        $j++;
                    }
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取自定义菜单成功',$menu);
    }
}